package xxl.signals.impl.natives.sets 
{
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	import xxl.signals.api.natives.INativeDispatcher;
	import xxl.signals.impl.natives.NativeRelaySignal;
	import xxl.signals.impl.natives.NativeSignal;
	/**
	 * ...
	 * @author XXL
	 */
	public class NativeSignalSet 
	{
		protected var target:IEventDispatcher;
		protected var _signals:Dictionary = new Dictionary();
		protected var maybeRepeat:Boolean;
		
		/**
		 * 
		 * @param	target
		 * @param	maybeRepeat 默认使用NativeSignal，如果会对同一个事件注册多个不同的listener，则建议将此属性设置为true，使用NativeRelaySignal将会提高性能。
		 */
		public function NativeSignalSet(target:IEventDispatcher, maybeRepeat:Boolean = false)
		{
			this.target = target;
			this.maybeRepeat = maybeRepeat;
		}
		
		public function getNativeSignal(eventType:String, eventClass:Class = null, useCapture:Boolean = false):INativeDispatcher
		{
			if (eventType == null)
			{
				throw new ArgumentError("eventType不能为空");
			}
			const flag:Boolean = maybeRepeat;
			const key:String = eventType + "_" + useCapture;
			if (!_signals[key])
			{
				if (flag)
				{
					_signals[key] = new NativeRelaySignal(target, eventType, eventClass, useCapture);
				}
				else
				{
					_signals[key] = new NativeSignal(target, eventType, eventClass, useCapture);
				}
			}
			return _signals[key];
		}
		
		public function removeAll():void
		{
			for each(var signal:INativeDispatcher in _signals)
			{
				signal.removeAll();
			}
			_signals = new Dictionary();
		}
		
	}

}